﻿using System;
using System.Collections.Generic;
using Foxit.PDF;
using Foxit.PDF.IO;
using Foxit.PDF.PageElements;
using Sustainalytics.Entities.RiskRatings;
using Sustainalytics.Reporting.Framework;

namespace Sustainalytics.RiskRatings.Reporting
{
    public class RiskRating_CustomMiniBulletChart : PageElement
    {
        public float Left { get; set; }
        public float Top { get; set; }
        public string Assessment { get; set;}
        public float Width { get; set; }
        public float Height { get; set; }

        private Group Elements()
        {
            var elements = new Group();

            float currentYOffset = Top;
            float startingPos;

            Dictionary<int, RgbColor> assessmentColors = new Dictionary<int, RgbColor>()
            {
                {1,  new RgbColor(204, 204, 204)},
                {2,  new RgbColor(248, 221, 152)},
                {3,  new RgbColor(245, 204, 101)},
                {4,  new RgbColor(243, 188, 23)},
                {5,  new RgbColor(240, 171, 0)}
            };

            Dictionary<string, int> assesmentsCode =new Dictionary<string, int>()
            {
                {"Negligible",1},
                {"Low", 2},
                {"Medium", 3},
                {"High", 4},
                {"Severe", 5}
            };

            currentYOffset += 3f;

            startingPos = Left+5f;

            foreach (var assesment in assesmentsCode)
            {
                if (Assessment == assesment.Key) {
                    for (var i = 1; i <= assesment.Value; i++) {
                        var borderColor = assessmentColors[i];
                        var borderStep = 0f;
                        if (i == assesment.Value)
                        {
                            borderColor = RgbColor.Black;
                            borderStep += 1f;
                        }
                        elements.Add(new Foxit.PDF.PageElements.Rectangle(startingPos, currentYOffset, 10f, 15f,
                         borderColor, assessmentColors[i], 1f));
                        startingPos += 10f+borderStep;

                    }
                    for (var j = assesment.Value + 1; j <= 5; j++)
                    {
                        elements.Add(new Foxit.PDF.PageElements.Rectangle(startingPos, currentYOffset, 10f, 15f,
                          assessmentColors[1], RgbColor.White, 0.5f));
                        startingPos += 10f;
                    }
                }
            }

            return elements;
    }
        public override void Draw(PageWriter writer)
        {
            var elements = Elements();
            writer.Write_q_(true);
            elements.Draw(writer);
            writer.Write_Q(true);
        }
    }
}

